package edu.virginia.cs4501.team22;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.TabActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import edu.virginia.cs4501.team22.objects.Event;
/**
 * Selects a building and displays a map with overlayed route.
 * 
 * @author William Schoen
 * @author Michael Parker
 */
public class ServiceOne extends Activity {
	private Spinner building_spinner;
	private EditText roomBox;
	private Button info;
	private TextView buildingInfo;

	private ClassFinderApp application;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try{
			super.onCreate(savedInstanceState);

			setContentView(R.layout.vertfirst);
			building_spinner = (Spinner) findViewById(R.id.buildings);
			roomBox = (EditText) findViewById(R.id.roomNumber);
			info = (Button) findViewById(R.id.info);
			buildingInfo = (TextView) findViewById(R.id.buildinginfo);
			buildingInfo.setMovementMethod(new ScrollingMovementMethod());

			//This loads the array of Building Names into the spin box
			ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
					this, R.array.building_array, android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			building_spinner.setAdapter(adapter);

			application = (ClassFinderApp)getApplication();
		}
		catch (Throwable t) {
			Toast.makeText(getApplicationContext(), t.toString(), Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onDestroy() {
		building_spinner = null;
		roomBox = null;
		info = null;
		buildingInfo = null;
		super.onDestroy();
	}

	public void setManualDestination(View view) {
		String buildingName = building_spinner.getItemAtPosition(building_spinner.getSelectedItemPosition()).toString();
		String room = roomBox.getText().toString();
		if (room.compareTo("Room")==0)
			room = "00";
		application.setEventNow(new Event("Manual Event", buildingName, room), false);
		((TabActivity)getParent()).getTabHost().setCurrentTab(1);
	}

	public void displayInfo(View view) {
		String buildingName = building_spinner.getItemAtPosition(building_spinner.getSelectedItemPosition()).toString();
		new GetBuildingInformationTask().execute(buildingName);
	}

	private class GetBuildingInformationTask extends AsyncTask<String, Void, String> {

		private static final String BI_METHOD_NAME = "findKeyword";
		private static final String BI_SOAP_ACTION = "http://stardock.cs.virginia.edu:8080/team3_buildings/services/BuildingsPort";
		private static final String BI_NAMESPACE = "http://megatron/";
		private static final String BI_URL = "http://stardock.cs.virginia.edu:8080/team3_buildings/services/BuildingsPort?wsdl";

		@Override
		protected String doInBackground(String... buildingName) {
			SoapObject request = new SoapObject(BI_NAMESPACE, BI_METHOD_NAME);
			request.addProperty("arg0", buildingName[0]);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(BI_URL);

			Object result = null;

			try {
				androidHttpTransport.call(BI_SOAP_ACTION, envelope);
				result = envelope.getResponse();
			} catch (Throwable t) {
			}
			envelope = null;
			androidHttpTransport = null;
			return result.toString();
		}

		@Override
		protected void onPostExecute(String result) {
			buildingInfo.setText(result);
		}
	}
}